layui.use(["zlContext"], function (exports) {
    var $ = layui.$
        , zlContext = layui.zlContext
        , layer = layui.layer
        , table = layui.table
        , form = layui.form
        , zlConfig = layui.zlConfig
        , statusName = zlConfig.global.result.statusName
        , msgName = zlConfig.global.result.msgName
        , dataName = zlConfig.global.result.dataName
        , OK = zlConfig.global.result.okState
        , fullheight = 'full-30'
        , loadingLayer
        , curLargeScreen = undefined
        , baseUrl = layui.cache['contentPath'] + '/manager/coremodelshow/?modelId=';

    //加载首页页面内容
    loadingLayer = layer.msg('数据加载中，请稍候...', { icon: 16, time: false, shade: 0.1 });
    var largescreenRwid = $.trim($('#largescreen-rwid').val());
    var OBJNAME = $('input[name=OBJNAME]').val();
    var LS_CONTENT = layui.cache['LS_CONTENT'];
    if(LS_CONTENT){
        curLargeScreen = JSON.parse(LS_CONTENT);
    }
    if(OBJNAME){
        $('title').text(OBJNAME);   
    }
    if(largescreenRwid){
        zlContext.asyncGet(layui.cache['contentPath'] + '/manager/largescreen/querybyid', { 'RWID': largescreenRwid}, function (result) {
            if (result[statusName] = OK) {
                curLargeScreen = result[dataName];
                $('title').text(curLargeScreen['OBJNAME']);
                if(curLargeScreen['LS_CONTENT']){
                    curLargeScreen = JSON.parse(curLargeScreen['LS_CONTENT']);
                } 
            } else {
                zlContext.errorMsg(result[msgName]);
            }
        }, false);
    }

    var options = {
        float: false,
        auto: true,
        always_show_resize_handle: false,
        disableDrag: true,
        disableResize: true
    };
    var frmTemplate = [
        '<div>'
        , '  <div class="grid-stack-item-content">'
        // , '     <div class="grid-stack-item-header"><h5 class="item-title">标题</h5>'
        // , '         <div class="grid-stack-item-operate">'
        // , '             <button class="layui-btn layui-btn-primary layui-btn-sm edit-btn"><i class="layui-icon layui-icon-edit"></i></button>'
        // , '             <button class="layui-btn layui-btn-primary layui-btn-sm del-btn"><i class="layui-icon layui-icon-delete"></i></button>'
        // , '         </div>'
        // , '     </div>'
        , '     <div class="grid-stack-item-body"></div>'
        , '  </div>'
        , '</div>'
    ];
    var defaultNode = {
        x: 0,
        y: 0,
        width: 4,
        height: 3
    }

    //渲染
    $('.grid-stack').gridstack(options);
    var lsGridstack = $('.grid-stack').data('gridstack');

    //数据展示
    if (curLargeScreen && curLargeScreen.length > 0) {
        try {            
            var url="";
            if(curLargeScreen[0].hasOwnProperty('theme')){
                var cssObj =curLargeScreen[0].theme;
                url = layui.cache['contentPath'] + '/static/libs/formdesign/css/bigscreenTheme/'+cssObj+'.css';
            }
            $('#theme-css').attr("href",url);
            $.each(curLargeScreen, function(index, node){
                var rwid = node['zlRwid'];
                var titleName = node['titleName'] || '标题';
                var $item = $(frmTemplate.join(''));
                node['locked'] = true;
                node['noMove'] = true;
                node['noResize'] = true;
                $item.attr('data-gs-locked', node.locked);
                $item.attr('data-gs-no-move', node.noMove); 
                $item.attr('data-gs-no-resize', node.noResize); 

                var url = baseUrl + rwid;
                $item.find('.grid-stack-item-content').attr('data-zl-rwid', rwid);
                // $item.find('.grid-stack-item-header .item-title').text(titleName);
                $item.find('.grid-stack-item-body').append('<iframe class="zl-ls-iframe" src="' + url + '" width=100% height=98%></iframe>');
                var iframe = $item.find('iframe.zl-ls-iframe')[0];
                iframe.onload = iframe.onreadystatechange = function iframeload() {
                    var $doc = $(this.contentWindow.document);
                    // $doc.find('html').css('background-color','#333');
                }
                lsGridstack.addWidget($item, node['x'], node['y'], node['width'],node['height'], 
                    node['autoPosition'], node['minWidth'], node['maxWidth'], node['minHeight'], node['maxHeight'], node['id']);
            });

        } catch (error) {
            zlContext.errorMsg('数据加载异常！！');
        }
       
    }
    $(".grid-stack").css({ "height": $(window).height() - 63, "min-height": $(window).height() - 140 });
    layer.close(loadingLayer);
    // z自适应控制页面高度
    $(window).resize(function () {
        var height = $(window).height() - 63;
        $("body").css("min-height", $(window).height() - 90);
        $(".grid-stack").css({ "height": height, "min-height": $(window).height() - 140 });
    });
});